package atm;

import java.sql.*;

public class SqlUtil {
    public Connection getConnection() {
        Connection con = null;
        // 1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/atm?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "Doksql721.");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }


    public User getByUsernameAndPwd(String username, String pwd) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;

        String sql = "select * from t_bank where b_username=? and b_userpsw=? ";
        try {
            //2.获取执行SQL语句的对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    public User getById(int id) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;

        String sql = "select * from t_bank where b_id=? ";
        try {
            //2.获取执行SQL语句的对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }


    public User getByUsername(String username) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;

        String sql = "select * from t_bank where b_username=? ";
        try {
            //2.获取执行SQL语句的对象,此处为prepareStatement,要预编译
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            //3.执行SQL并返回结果集
            rs = statement.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("b_id"));
                user.setAccount(rs.getString("b_username"));
                user.setPassword(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;

    }

    public void update(User user) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_bank set b_userpsw=? , b_money=? where b_id=? ";
        try {
            con.setAutoCommit(false);
            //2.获取执行SQL语句对象,此处为prepareStatement,要预编译
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPassword());
            statement.setObject(2, user.getMoney());
            statement.setObject(3, user.getId());
            //3.执行SQL语句
            int row = statement.executeUpdate();

            con.commit();
            System.out.println("影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            //4.关闭连接
            close(con, statement);
        }

    }

    public void update(User loginuser, User otheruser) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_bank set b_userpsw=? , b_money=? where b_id=? ";
        try {
            con.setAutoCommit(false);
            //2.获取执行SQL语句对象,此处为prepareStatement,要预编译
            statement = con.prepareStatement(sql);
            statement.setObject(1, loginuser.getPassword());
            statement.setObject(2, loginuser.getMoney());
            statement.setObject(3, loginuser.getId());
            statement.executeUpdate();

            statement = con.prepareStatement(sql);
            statement.setObject(1,otheruser.getPassword());
            statement.setObject(2,otheruser.getMoney());
            statement.setObject(3,otheruser.getId());
            statement.executeUpdate();

            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            //4.关闭连接
            close(con, statement);
        }

    }


    //关闭添加
    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //关闭查询
    public void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
